package com.tcu.boiler.factory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcu.boiler.factory.entity.BoilerManufacturer;
import com.tcu.boiler.factory.entity.Vo.BoilerManu;
import com.tcu.boiler.factory.service.IBoilerManufacturerService;
import com.tcu.exception.SmartException;
import com.tcu.result.R;
import org.mockito.internal.matchers.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/boiler-manufacturer")
public class BoilerManufacturerController {
    private IBoilerManufacturerService manufacturerService;

    @PostMapping("add")
    public R add(@RequestBody BoilerManufacturer manufacturer){
        if (manufacturer.getId()!=null) {
            throw new SmartException(20001,"id不为空");
        }
        boolean save = manufacturerService.save(manufacturer);

        return save?R.success():R.error();
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = manufacturerService.removeById(id);
        return b?R.success():R.error();
    }

    @PostMapping("update")
    public R update(@RequestBody BoilerManufacturer manufacturer){
        if (manufacturer.getId()==null) {
            throw new SmartException(20001,"id为空");
        }
        boolean b = manufacturerService.updateById(manufacturer);
        return b?R.success():R.error();
    }

    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        BoilerManufacturer byId = manufacturerService.getById(id);
        return R.success().data("boilerManufacturer", byId);
    }

    @PostMapping("listCondition/{page}/{size}")
    public R listCondition(@PathVariable long page, @PathVariable long size,@RequestBody BoilerManu manu){
        NotNull notNull = NotNull.NOT_NULL;
        Page<BoilerManufacturer> p = new Page<>(page,size);
        QueryWrapper<BoilerManufacturer> wrapper = new QueryWrapper<>();

            wrapper.like(notNull.matches(manu.getFactoryLocLike()),"factory_loc",manu.getFactoryLocLike())
                    .like(notNull.matches(manu.getDirectorNameLike()),"director_name",manu.getDirectorNameLike())
                    .like(notNull.matches(manu.getFactoryNameLike()),"factory_name",manu.getFactoryNameLike());
        manufacturerService.page(p,wrapper);
        return R.success().data("pageInfo",p);
    }

    @GetMapping("list")
    public R list(){
        List<BoilerManufacturer> list = manufacturerService.list();
        return R.success().data("list", list);
    }





    @Autowired
    public void setManufacturerService(IBoilerManufacturerService manufacturerService) {
        this.manufacturerService = manufacturerService;
    }
}
